﻿using System;
using System.Data;
using System.Reflection;


public abstract class Standard_Info
{
    public void SetPropertyValue(string property, object value)
    {
        PropertyInfo pi = this.GetType().GetProperty(property);
        pi.SetValue(this, Convert.ChangeType(value, pi.PropertyType), null);
    }

    public object GetPropertyValue(string property)
    {
        PropertyInfo pi = this.GetType().GetProperty(property);
        return pi.GetValue(this, null);
    }

    public T GetPropertyValue<T>(string property)
    {
        PropertyInfo pi = this.GetType().GetProperty(property);
        return (T)pi.GetValue(this, null);
    }

    public Type GetPropertyType(string property)
    {
        PropertyInfo pi = this.GetType().GetProperty(property);
        return pi.PropertyType;
    }

    public bool HasProperty(string property)
    {
        PropertyInfo pi = this.GetType().GetProperty(property);
        if (pi != null)
            return true;
        return false;
    }

    public void FillDataProperty(DataRow row)
    {
        foreach (DataColumn col in row.Table.Columns)
        {
            if (HasProperty(col.ColumnName))
            {
                if (row[col.ColumnName] is DBNull || row[col.ColumnName] == null)
                {
                    if (col.DataType == typeof(int)
                        || col.DataType == typeof(Int16)
                        || col.DataType == typeof(Int32)
                        || col.DataType == typeof(Int64)
                        || col.DataType == typeof(UInt16)
                        || col.DataType == typeof(UInt32)
                        || col.DataType == typeof(UInt64)
                        || col.DataType == typeof(decimal)
                        || col.DataType == typeof(double)
                        || col.DataType == typeof(float)
                        || col.DataType == typeof(byte)
                        || col.DataType == typeof(sbyte))
                        SetPropertyValue(col.ColumnName, 0);

                    else if (col.DataType == typeof(DateTime))
                        SetPropertyValue(col.ColumnName, DateTime.MinValue);

                    else if (col.DataType == typeof(Boolean))
                        SetPropertyValue(col.ColumnName, false);

                    else if (col.DataType == typeof(String))
                        SetPropertyValue(col.ColumnName, "");
                    else
                        SetPropertyValue(col.ColumnName, "");
                }
                else
                    SetPropertyValue(col.ColumnName, row[col.ColumnName]);
            }
        }
    }
}
